Data

libraries

#install.packages('catchment')
#library(catchment)
library(sf)
## Warning: package 'sf' was built under R version 4.0.5
## Linking to GEOS 3.9.1, GDAL 3.4.0, PROJ 8.1.1; sf_use_s2() is TRUE
library(leaflet)
library(dplyr)
#library(catchment)

start new

 rm(list=ls())
library(tidycensus)
library(tidyverse)

census_api_key("eba406410c653b81d6a795ac4e989221f7bdf302")
## To install your API key for use in future sessions, run this function with `install = TRUE`.
pop_county <- get_acs(geography = "tract", 
                  year = 2020,
                  variables = c(population = "B01001_001", 
                                mean_income = 'B19013_001'),
                  state = c("VA"),
                  county = c("Fairfax county"),
                  survey = "acs5",
                  output = "wide",
                  geometry = TRUE)
## Getting data from the 2016-2020 5-year ACS
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
#View(pop_county)
#tract shapes
tract_shapes <-   st_transform( tigris::tracts(state = "VA", county = "Fairfax county", class = "sf", year=2020), 4326)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |======================================================================| 100%
library(dplyr)
pal <- colorBin("RdYlBu", pop_county$mean_incomeE, bins = 10)

#create map
map <- leaflet(tract_shapes, options = leafletOptions(attributionControl = FALSE)) %>%
  addTiles() %>%
  addScaleBar("bottomleft") %>%
  addPolylines(data = tract_shapes, color = "black", opacity = 1, weight = 1) %>%
  addLayersControl(
    position = "topleft", overlayGroups = c("Population", "Income")
  )  %>%
  
  addPolygons(
    fillColor = colorNumeric("RdYlBu", pop_county$populationE)(pop_county$populationE),
    fillOpacity = 1, stroke = FALSE, group = "Population", label = pop_county$populationE
  ) %>%
  hideGroup("Population") %>%
  addControl("Income per census tract, Fairfax County, VA", "topright") %>%
  addLegend("bottomright", pal, pop_county$mean_incomeE, opacity = .7) %>%
  addPolygons(
    fillColor = pal(pop_county$mean_incomeE), fillOpacity = .7, weight = 1, color = "#000",
    highlightOptions = highlightOptions(color = "#fff"), group = "Income",
    label = paste0(
      "GEOID: ", pop_county$GEOID, ", Population: ", pop_county$populationE,
      ", Median Income: ", round(pop_county$mean_incomeE, 4)
    )
  )

map
leaflet() %>%
  addTiles(group = "OpenStreetMap") %>%
  addProviderTiles("Stamen.Toner", group = "Toner by Stamen") %>%
  addMarkers(runif(20, -75, -74), runif(20, 41, 42), group = "Markers") %>%
  addLayersControl(
    baseGroups = c("OpenStreetMap", "Toner by Stamen"),
    overlayGroups = c("Markers")
  )